package com.anysoft.ui.activity;

import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.base.BaseBaseActivity;
import com.anysoft.callback.StringDialogCallback;
import com.anysoft.config.AppConstant;
import com.anysoft.config.HostConstant;
import com.anysoft.model.AccountLoginEntity;
import com.anysoft.ui.presenter.LoginPresenter;
import com.anysoft.utils.ActivityCollector;
import com.anysoft.utils.PreferencesUtil;
import com.anysoft.utils.StatusBarUtil;
import com.anysoft.utils.StringUtil;
import com.anysoft.utils.ToastUtil;
import com.anysoft.widget.HWEditText;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by LiTingYao on 2018/5/24.
 */
public class LoginActivity extends BaseBaseActivity {
    
    @BindView(R.id.tv_login)
    TextView tv_login;
    @BindView(R.id.et_login_mobile)
    HWEditText et_login_mobile;
    @BindView(R.id.et_login_password)
    HWEditText et_login_password;
    @BindView(R.id.cb_logined_remember)
    CheckBox cb_logined_remember;
    @BindView(R.id.tv_login_forget_pw)
    TextView tv_login_forget_pw;
    @BindView(R.id.tv_online_regist)
    TextView tv_online_regist;
    
    private String mobile;
    private String password;
    private Handler mHandler = new Handler();
    
    @OnClick({R.id.tv_login, R.id.tv_login_forget_pw, R.id.tv_online_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                mobile = et_login_mobile.getText().toString().trim();
                password = et_login_password.getText().toString().trim();
                if (checkParam()) {
                    getLoginData(mobile, password);
                }
                break;
            case R.id.tv_login_forget_pw:
                
                break;
            case R.id.tv_online_regist:
                
                break;
        }
    }
    
    private boolean checkParam() {
        if (StringUtil.isEmpty(mobile)) {
            ToastUtil.showShort("手机号不能为空");
            return false;
        }
        if (StringUtil.isEmpty(password)) {
            ToastUtil.showShort("密码不能为空");
            return false;
        }
        return true;
    }
    
    public void getLoginData(String mobile, String password) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("password", password);
        OkGo.<String>post(HostConstant.LOGIN)
                .tag(this)
                .upJson(new Gson().toJson(params))
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        getLoginDataSuccess(response.body());
                    }
                    
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showShort("服务器忙...");
                    }
                    
                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
        
    }
    
    public void getLoginDataSuccess(String json) {
        AccountLoginEntity loginEntity = null;
        try {
            loginEntity = new Gson().fromJson(json, AccountLoginEntity.class);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        
        if ("0".equals(loginEntity.getCode())) {
            ToastUtil.showShort("登录成功");
            PreferencesUtil.putBoolean(this, AppConstant.REMBER_MOBILE, cb_logined_remember.isChecked());
            PreferencesUtil.putString(this, AppConstant.MOBILE_NUMBER, et_login_mobile.getText().toString().trim());
            userInfoManager.saveUserInfo(loginEntity);
            AccountLoginEntity.UserInfoBean userInfo = loginEntity.getUserInfo();
            String userType = userInfo.getUserType();
            if ("manager".equals(userType)) {
                ActivityCollector.startTargetActivity(this, MsMainHomeActivity.class);
            } else if ("mentor".equals(userType)) {
                ActivityCollector.startTargetActivity(this, MentorHomeActivity.class);
            } else if ("parent".equals(userType)) {
                ActivityCollector.startTargetActivity(this, ParentHomeActivity.class);
            }
            finish();
        } else {
            ToastUtil.showShort("登录失败: " + loginEntity.getMsg());
        }
    }
    
    @Override
    protected void doBeforeSetContentView() {
        super.doBeforeSetContentView();
        StatusBarUtil.fullScreen(this);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    }
    
    @Override
    protected LoginPresenter createPresenter() {
        return null;
    }
    
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_login_layout;
    }
    
    @Override
    public void initView() {
        super.initView();
        if (PreferencesUtil.getBoolean(this, AppConstant.REMBER_MOBILE)) {
            String mobileNum = PreferencesUtil.getString(this, AppConstant.MOBILE_NUMBER);
            if (!StringUtil.isEmpty(mobileNum)) {
                et_login_mobile.setText(mobileNum);
                et_login_mobile.setSelection(mobileNum.length());
            }
        }
    }
    
    @Override
    public void initData() {
        super.initData();
    }
    
    @Override
    public void initListener() {
        super.initListener();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
